using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MeetFuture.MRC
{
	public class clsToolInfo
	{
        private const string key_ToolID = "TOOLID=";
        private const string key_ToolStatus = "TOOLSTATUS=";
        private const string key_PortList = "PORTLIST=";
        private const string key_split = ",";

        private string _ToolID = string.Empty;
        private string _ToolStatus = string.Empty;
        private Dictionary<string, clsPortInfo> _MESPorts = new Dictionary<string, clsPortInfo>();
        private Dictionary<string, clsPortInfo> _EAPPorts = new Dictionary<string, clsPortInfo>();

        private List<string> _EmptyLots = new List<string>();

        public clsToolInfo(string ToolID)
        {
            this._ToolID = ToolID;
            this._ToolStatus = string.Empty;
            this._MESPorts = new Dictionary<string, clsPortInfo>();
            this._EAPPorts = new Dictionary<string, clsPortInfo>();
            this._EmptyLots = new List<string>();
        }

        public string ToolID
        {
            get { return this._ToolID; }
        }
        public string ToolStatus
        {
            get { return this._ToolStatus; }
            set 
            {
                this._ToolStatus = ApplicationGlobal.GetToolStatusCode(value); 
            }
        }
        public Dictionary<string, clsPortInfo> MESPorts
        {
            get { return this._MESPorts; }
        }
        public Dictionary<string, clsPortInfo> EAPPorts
        {
            get { return this._EAPPorts; }
        }
        public List<string> EmptyLots
        {
            get { return this._EmptyLots; }
            set { this._EmptyLots = value; }
        }
        public void AddMESPort(clsPortInfo MESPort)
        {
            try
            {
                clsPortInfo tmpMESPort = null;
                this._MESPorts.TryGetValue(MESPort.PortID, out tmpMESPort);
                if (tmpMESPort == null)
                {
                    this._MESPorts.Add(MESPort.PortID, MESPort);
                } 
                else
                {
                    this._MESPorts[MESPort.PortID] = MESPort;
                }
            }
            catch (System.Exception ex)
            {	
            }
        }
        public void AddEAPPort(clsPortInfo EAPPort)
        {
            try
            {
                clsPortInfo tmpEAPPort = null;
                this._EAPPorts.TryGetValue(EAPPort.PortID, out tmpEAPPort);
                if (tmpEAPPort == null)
                {
                    this._EAPPorts.Add(EAPPort.PortID, EAPPort);
                }
                else
                {
                    this._EAPPorts[EAPPort.PortID] = EAPPort;
                }
            }
            catch (System.Exception ex)
            {
            }
        }
        public clsPortInfo Get_MESPort_ByPortID(string PortID)
        {
            clsPortInfo result = null;
            try
            {
                this._MESPorts.TryGetValue(PortID, out result);
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }
        public clsPortInfo Get_EAPPort_ByPortID(string PortID)
        {
            clsPortInfo result = null;
            try
            {
                this._EAPPorts.TryGetValue(PortID, out result);
            }
            catch (System.Exception ex)
            {
                result = null;
            }
            return result;
        }
        public string GetPortIDByLotID(string LotId)
        {
            string result = string.Empty;
            try
            {
            	foreach (clsPortInfo port in this._EAPPorts.Values)
            	{
            	    if (port.LotID.ToUpper().Equals(LotId.ToUpper()))
            	    {
                        result = port.PortID;
                        break;
            	    }
            	}
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }
        public void UpdateMESPortInfo(clsPortInfo Port)
        {
            try
            {
                this.AddMESPort(Port);                
            }
            catch (System.Exception ex)
            {	
            }
        }
        public void UpdateEAPPortInfo(clsPortInfo Port)
        {
            try
            {
                this.AddEAPPort(Port);                        
            }
            catch (System.Exception ex)
            {
            }
        }        
        public void UpdateMESPortInfo(string Port_ID)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Port_ID))
                {
                    return;
                }
                clsPortInfo Port = new clsPortInfo(Port_ID);
                clsPortInfo current = null;
                this._MESPorts.TryGetValue(Port_ID, out current);
                if (current != null)
                {
                    current.LotID = string.Empty;
                }
                else
                {
                    this._MESPorts.Add(Port_ID, Port);
                }                
            }
            catch (System.Exception ex)
            {
            }
        }
        public void UpdateEAPPortInfo(string Port_ID)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Port_ID))
                {
                    return;
                }
                clsPortInfo Port = new clsPortInfo(Port_ID);
                clsPortInfo current = null;
                this._EAPPorts.TryGetValue(Port_ID, out current);
                if (current != null)
                {
                    current.LotID = string.Empty;
                }
                else
                {
                    this._EAPPorts.Add(Port_ID, Port);
                }                
            }
            catch (System.Exception ex)
            {
            }
        }
        public void RemovePort(string Port_ID)
        {
            try
            {
                this.UpdateMESPortInfo(Port_ID);
                this.UpdateEAPPortInfo(Port_ID);
            }
            catch (System.Exception ex)
            {
            }
        }
        public void RemoveMESPorts()
        {
            try
            {
                Dictionary<string, clsPortInfo> tmp_MESPorts = new Dictionary<string, clsPortInfo>();
                foreach (string portId in this._MESPorts.Keys)
                {
                    clsPortInfo Port = new clsPortInfo(portId);
                    tmp_MESPorts.Add(portId, Port);
                }
                this._MESPorts = tmp_MESPorts;
            }
            catch (System.Exception ex)
            {
            }
        }
        public void RemoveEAPPorts()
        {
            try
            {
                Dictionary<string, clsPortInfo> tmp_EAPPorts = new Dictionary<string, clsPortInfo>();
                foreach (string portId in this._EAPPorts.Keys)
                {
                    clsPortInfo Port = new clsPortInfo(portId);
                    tmp_EAPPorts.Add(portId, Port);
                }
                this._EAPPorts = tmp_EAPPorts;
            }
            catch (System.Exception ex)
            {
            }
        }      
        public string ToObjectString()
        {
            string result = string.Empty;
            try
            {
                result += "[";
                result += key_ToolID + this._ToolID + key_split;
                result += key_ToolStatus + this._ToolStatus + key_split;
                result += key_PortList + "[";
                foreach (clsPortInfo port in this._MESPorts.Values)
                {
                    clsPortInfo EAPPort = null;
                    this._EAPPorts.TryGetValue(port.PortID, out EAPPort);
                    if (EAPPort != null)
                    {
                        port.LotID = EAPPort.LotID;
                    }
                    result += port.ToObjectString();
                }
                result += "]";
                result += "]";
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }
        public override string ToString()
        {
            return this._ToolID;
        }
	}
}
